package com.weiyi.mashangzhu.easemob.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.weiyi.mashangzhu.Application.MyApp;

import java.util.ArrayList;
import java.util.List;

public class ChatManager {

    private static ChatManager instance;
    private final ChatHelper chatHelper;

    private ChatManager() {
        chatHelper = ChatHelper.getInstance(MyApp.getAppContext().getApplicationContext());
    }

    public static ChatManager getInstance(){
        if(instance == null){
            synchronized(ChatManager.class) {
                instance = new ChatManager();
            }
        }
        return instance;
    }

    public void add(String account, String nick, String avatar) {
        SQLiteDatabase rdb = chatHelper.getReadableDatabase();
        ContentValues cv = new ContentValues();
        cv.putNull("_id");
        cv.put(ChatDao.ACCOUNT, account);
        cv.put(ChatDao.NICK, nick);
        cv.put(ChatDao.AVATAR, avatar);
        rdb.insert(ChatDao.TABLE_NAME, "", cv); // 不等于-1成功
    }

    public void delete(String account) {
        SQLiteDatabase rdb = chatHelper.getReadableDatabase();
        rdb.delete(ChatDao.TABLE_NAME, ChatDao.ACCOUNT + " = ?", new String[]{account}); // 大于0成功
    }

    public void update(String account, String nick, String avatar) {
        if (!"".equals(nick) || !"".equals(avatar)) {
            SQLiteDatabase rdb = chatHelper.getReadableDatabase();
            ContentValues cv = new ContentValues();
            if (!"".equals(nick)) {
                cv.put(ChatDao.NICK, nick);
            }
            if (!"".equals(avatar)) {
                cv.put(ChatDao.AVATAR, avatar);
            }
            rdb.update(ChatDao.TABLE_NAME, cv, ChatDao.ACCOUNT + " = ?", new String[]{account}); // 大于0成功
        }
    }

    public boolean isExist(String account) {
        SQLiteDatabase rdb = chatHelper.getReadableDatabase();
        Cursor c = rdb.query(ChatDao.TABLE_NAME, null, ChatDao.ACCOUNT + " = ?", new String[]{account}, null, null, null, null);
        if (c.moveToNext()) {
            c.close();
            return true;
        }
        c.close();
        return false;
    }

    public String[] findByAccount(String account) {
        SQLiteDatabase rdb = chatHelper.getReadableDatabase();
        String[] nickAndAvatar = new String[2];
        Cursor c = rdb.query(ChatDao.TABLE_NAME, null, ChatDao.ACCOUNT + " = ?", new String[]{account}, null, null, null, null);
        while (c.moveToNext()) {
            nickAndAvatar[0] = c.getString(c.getColumnIndex(ChatDao.NICK));
            nickAndAvatar[1] = c.getString(c.getColumnIndex(ChatDao.AVATAR));
        }
        c.close();
        return nickAndAvatar;
    }

    public List<ChatBean> findAll() {
        SQLiteDatabase rdb = chatHelper.getReadableDatabase();
        List<ChatBean> chatList = new ArrayList<>();
        Cursor c = rdb.query(ChatDao.TABLE_NAME, null, null, null, null, null, null, null);
        while (c.moveToNext()) {
            ChatBean chatBean = new ChatBean();
            chatBean.setAccount(c.getString(c.getColumnIndex(ChatDao.ACCOUNT)));
            chatBean.setNick(c.getString(c.getColumnIndex(ChatDao.NICK)));
            chatBean.setAvatar(c.getString(c.getColumnIndex(ChatDao.AVATAR)));
            chatList.add(chatBean);
        }
        c.close();
        return chatList;
    }
}
